package cn.flying.cloud.flow.core.listener.event.callback;


import javax.annotation.Resource;

import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.event.impl.FlowableActivityEventImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import cn.flying.cloud.flow.core.enums.FlowStateEnum;

/**
 * 执行节点完成监听
 *
 * @author: admin
 * @date: 2023年02月26日 08:23
 * @version: 1.0
 */
@Component
public class ActivityCompletedListenerCallback implements EventListenerCallback {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private RuntimeService runtimeService;

    /**
     * @param event
     */
    @Override
    public void execute(FlowableEvent event) {
        logger.info("==========活动完成监听器回调处理---start===========");
        // FlowableEngineEventImpl
        logger.info("监听事件类型：【{}】", event.getType().name());
        FlowableActivityEventImpl entityEvent = (FlowableActivityEventImpl) event;
        String behaviorClass = entityEvent.getBehaviorClass();

        // 非开始节点、结束节点执行，流程为审批中
        if (!"startEvent".equals(entityEvent.getActivityType()) && !"endEvent".equals(entityEvent.getActivityType())) {
            runtimeService.updateBusinessStatus(entityEvent.getProcessInstanceId(), FlowStateEnum.Running.getCode());
        }

        logger.info("活动名称：【{}】，活动ID：【{}】执行完成，流程：【{}】，实例ID：【{}】", entityEvent.getActivityName(), entityEvent.getActivityId(), entityEvent.getProcessDefinitionId(), entityEvent.getProcessInstanceId());
        logger.info("==========活动完成监听器回调处理---end===========");
    }
}
